<template>
  <div class='container'>
    <!-- 卡片板块 -->
    <el-card class="box-card">
      <div class="cont-fenju">
        <!-- 文字提示 -->
      <span class="car-span">说明：目前支持学科和关键字条件筛选</span>
      <!-- 新增试题按钮 -->
      <el-button type="success" size="small" icon="el-icon-edit" @click="luBtn">新增试题</el-button>
    </div>
    <!-- 表单板块 -->
      <el-form ref="form" v-model="queryInfo">
        <el-row>
          <el-col :span="6">
            <el-form-item label="学科" label-width="80px">
              <el-select style="width: 100%" v-model="queryInfo.subjectID" placeholder="请选择" @change="secondaryDirectory(queryInfo.subjectID)">
                <el-option v-for="(item,index) in simpleList" :key='index' :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="二级目录" label-width="80px">
              <el-select style="width: 100%" v-model="queryInfo.catalogID" placeholder="请选择" @change="getAble(queryInfo.subjectID)">
                <el-option v-for="item in second" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="标签" label-width="80px">
              <el-select style="width: 100%" v-model="queryInfo.tagName" placeholder="请选择">
                <el-option :label="item.label" :value="item.value" v-for="item in lableList" :key="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="关键字" label-width="80px">
              <el-input  style="width: 100%" v-model="queryInfo.keyword" placeholder="根据题干搜索"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="试题类型" label-width="80px">
              <el-select style="width: 100%" v-model="queryInfo.questionType" placeholder="请选择">
                <el-option :label="item.label" :value="item.value" v-for="item in questionType" :key="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="难度" label-width="80px">
              <el-select style="width: 100%" v-model="queryInfo.difficulty" placeholder="请选择">
                <el-option :label="item.label" :value="item.value" v-for="item in difficulty" :key="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="方向" label-width="80px">
              <el-select style="width: 100%" v-model="queryInfo.direction" placeholder="请选择">
                <el-option :label="item" :value="index" v-for="(item, index) in direction" :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="录入人" label-width="80px">
              <el-select style="width: 100%" v-model="queryInfo.creatorID" placeholder="请选择">
                <el-option :label="item.username" :value="item.id" v-for="item in CreatorIdList" :key="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row >
          <el-col :span="6">
            <el-form-item label="题目备注" label-width="80px" v-model="queryInfo.remarks">
              <el-input style="width: 100%"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="企业简称" label-width="80px" v-model="queryInfo.shortName">
              <el-input style="width: 100%"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="城市" label-width="80px">
              <el-select style="width: 40%" v-model="queryInfo.provinces" placeholder="请选择" @change="City(queryInfo.provinces)">
                <el-option :label="item" :value="item" v-for="(item, index) in provinces" :key="index"></el-option>
              </el-select>
              <el-select style="width: 50%" v-model="queryInfo.citys" placeholder="请选择">
                <el-option :label="item" :value="index" v-for="(item, index) in citys" :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
          </el-col>
          <el-col :span="6">
            <el-form-item label-width="80px">
              <el-button size="mini" @click="del">取消</el-button>
              <el-button type="primary" size="mini" @click="search">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    <!-- 标签页区域 -->
    <el-tabs  type="card" @tab-click="handleClick" v-model="activeName">
      <el-tab-pane label="全部" name="first">全部</el-tab-pane>
      <el-tab-pane label="已审核" name="second">已审核</el-tab-pane>
      <el-tab-pane label="待审核" name="third">待审核</el-tab-pane>
      <el-tab-pane label="已拒绝" name="fourth">已拒绝</el-tab-pane>
    </el-tabs>
      <!--     提示文本-->
      <div>
        <el-alert
          type="info"
          show-icon>
          <span slot="title">数据一共{{total}}条</span>
        </el-alert>
      </div>
      <!-- 表格区域 -->
      <el-table
        border
        style="width: 100%" :data ='containerJx'>
        <el-table-column
          fixed
          prop="number"
          label="试题编号"
          width="150">
        </el-table-column>
        <el-table-column
          prop="subject"
          label="学科"
          width="120">
        </el-table-column>
        <el-table-column
          prop="catalog"
          label="目录"
          width="120">
        </el-table-column>
        <el-table-column
          prop="questionType"
          label="题型"
          width="120">
          <template slot-scope="scope">
            {{mydata2(scope.row.questionType)}}
          </template>
        </el-table-column>
        <el-table-column
          prop="question"
          label="题干"
          width="300">
          <template slot-scope="scope">
            <div v-html='scope.row.question'></div>
          </template>
        </el-table-column>
        <el-table-column
          prop="addDate"
          label="录入时间"
          width="180">
          <template slot-scope="scope">
            {{scope.row.addDate | parseTimeByString}}
          </template>
        </el-table-column>
        <el-table-column
          prop="difficulty"
          label="难度"
          width="120">
          <template slot-scope="scope">
            {{mydata(scope.row.difficulty)}}
          </template>
        </el-table-column>
        <el-table-column
          prop="creator"
          label="录入人"
          width="120">
        </el-table-column>
        <el-table-column
          prop="chkState"
          label="审核状态"
          width="120">
          <template slot-scope="scope">
            {{mydata3(scope.row.chkState)}}
          </template>
        </el-table-column>
        <el-table-column
          prop="chkRemarks"
          label="审核意见"
          width="150">
        </el-table-column>
        <el-table-column
          prop="chkUser"
          label="审核人"
          width="120">
        </el-table-column>
        <el-table-column
          prop="publishState"
          label="发布状态"
          width="120">
          <template slot-scope="scope">
            {{mydata4(scope.row.publishState)}}
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="200">
          <template slot-scope="scope">
            <el-button @click="previewEditDialog(scope.row.id)" type="text" size="small">预览</el-button>
            <el-button @click="auditEditDialog(scope.row)"  type="text" size="small" :disabled="diff(scope.row.chkState)">审核</el-button>
            <el-button @click="alterEditDialog(scope.row.id)" type="text" size="small" :disabled="scope.row.publishState === 1 ? true : false">修改</el-button>
            <el-button @click="groundingEditDialog(scope.row)"  type="text" size="small">{{trueOrFalse(scope.row.publishState)}}</el-button>
            <el-button @click="delList(scope.row.id)"  type="text" size="small" :disabled="scope.row.publishState === 1 ? true : false">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--      分页功能-->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>
    <!-- 预览弹出层 -->
    <el-dialog
      title="预览文章"
      :visible.sync="previewDialogVisible"
      width="50%">
      <span>
        <!-- <questions-preview :id="id"></questions-preview> -->
        123
        </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="previewDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="previewDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 审核弹出层 -->
    <el-dialog
      title="题目审核"
      :visible.sync="deleteDialogVisible"
      width="20%">
      <div>
        <el-radio v-model="form.chkState" :label="1">通过</el-radio>
        <el-radio v-model="form.chkState" :label="2">拒绝</el-radio>
      </div>
      <el-form ref="form" :model="queryInfo" style="margin-top: 15%">
        <el-form-item>
          <el-input type="textarea" v-model="form.chkRemarks" placeholder="请输入审核意见"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="toExamine">确 定</el-button>
      </span>
    </el-dialog>
    <!--题目预览区域  -->
    <el-dialog
      title="题目预览"
      :visible.sync="diaItenVisible"
      width="50%"
      v-if="diaItenVisible"
    >
      <questions-preview :id="rowId"></questions-preview>
      <span slot="footer" class="dialog-footer">
          <el-button @click="diaItenVisible = false" type="primary"
          >关闭</el-button
          >
        </span>
    </el-dialog>
  </div>
</template>

<script>

import { choice, remove, choiceCheck, choicePublish } from '@/api/hmmm/questions'
import { simple } from '@/api/hmmm/subjects'
import { simple as direct } from '@/api/hmmm/directorys'
import { simple as lable } from '@/api/hmmm/tags'
import { difficulty, questionType, direction, publishType, chkType } from '@/api/hmmm/constants'

import { simple as people } from '@/api/base/users'
import { citys, provinces } from '@/api/hmmm/citys'
import QuestionsPreview from '@/module-hmmm/components/questions-preview'

export default {
  name: 'Contsiner',
  // 注册组件
  components: {
    QuestionsPreview
  },
  props: {},
  data () {
    return {
      activeName: 'first',
      disable: false,
      form: {
      },
      // 每一行数据的id
      rowId: '',
      // 对话框的显示隐藏
      diaItenVisible: false,
      difficulty,
      questionType,
      direction,
      publishType,
      chkType,
      // 学科
      item: '',
      queryInfo: {
        page: 1,
        pagesize: 5
      },
      total: 0,
      // 简单的学科列表
      simpleList: [],
      // 二级目录
      second: [],
      // 标签列表
      lableList: [],
      // 录入人信息
      CreatorIdList: [],
      // 城市
      citys: [],
      provinces: provinces(),
      // 所有精选列表数据
      containerJx: [],
      // 预览文章弹出层显示与隐藏
      previewDialogVisible: false,
      // 审核文章弹出层显示与隐藏
      deleteDialogVisible: false
    }
  },
  // 计算属性
  computed: {
    // 控制审核按钮
    diff: function () {
      return function (val) {
        if (val === 2) {
          return true
        } else {
          return false
        }
      }
    },
    // 上下架
    trueOrFalse: function () {
      return function (val) {
        if (val === 1) {
          return '下架'
        } else {
          return '上架'
        }
      }
    },
    // 难度
    mydata: function () {
      return function (val) {
        if (val === 1) {
          return this.difficulty[0].label
        } else if (val === 2) {
          return this.difficulty[1].label
        } else {
          return this.difficulty[2].label
        }
      }
    },
    // 题型
    mydata2: function () {
      return function (val) {
        if (val === 1) {
          return this.questionType[0].label
        } else if (val === 2) {
          return this.questionType[1].label
        } else {
          return this.questionType[2].label
        }
      }
    },
    // 审核状态
    mydata3: function () {
      return function (val) {
        if (val === 0) {
          return '待审核'
        } else if (val === 1) {
          return '通过'
        } else {
          return '拒绝'
        }
      }
    },
    // 发布状态
    mydata4: function () {
      return function (val) {
        if (val === 0) {
          return '已发布'
        } else if (val === 1) {
          return '待发布'
        } else {
          return '已下架'
        }
      }
    }
  },
  watch: {},
  created () {
    // 发起网路请求获取数据
    this.getContsiner()
    this.getSleepList()
    this.getCreatorID()
    this.getJxList()
  },
  mounted () {},
  methods: {
    // 点击审核确定
    async toExamine () {
      await choiceCheck(this.form)
      this.getContsiner()
      this.deleteDialogVisible = false
      this.$message.success('审核成功')
      this.getJxList()
      console.log(this.form.chkState)
    },
    // 页面跳转
    luBtn () {
      this.$router.push('new')
    },
    async getJxList () {
      // 获取精选题库所有数据
      const { data: abb } = await choice(this.queryInfo)
      this.containerJx = abb.items
      this.total = abb.counts
      // console.log(this.)
      // console.log(abb)
      if (this.form.chkState === 2) {
        this.disable = true
      } else {
        this.disable = false
      }
    },
    // 获取学科数据
    async getContsiner () {
      // 获取筛选数据
      const { data: res } = await simple()
      const { data: abs } = await people()
      // 保存数据
      this.containerLu = abs
      this.containerForm = res
    },
    // tab栏事件
    async handleClick () {
      console.log(this.activeName)
      if (this.activeName === 'first') {
        return true
      } else if (this.activeName === 'second') {
        this.queryInfo.chkState = 1
        this.getJxList()
      } else if (this.activeName === 'third') {
        this.queryInfo.chkState = 0
        this.getJxList()
      } else {
        this.queryInfo.chkState = 2
        this.getJxList()
      }
    },
    // 预览按钮
    previewEditDialog (id) {
      this.rowId = id
      this.diaItenVisible = true
    },
    // 审核按钮
    auditEditDialog (data) {
      this.deleteDialogVisible = true
      this.form.id = data.id
      this.form = {
        ...data
      }
      console.log(this.form)
    },
    // 修改按钮
    alterEditDialog (id) {
      this.$router.push({ path: 'new', query: { id: id } })
    },
    // 上架按钮
    async groundingEditDialog (data) {
      const sta = data.publishState === 1 ? '0' : '1'

      await choicePublish({ id: data.id, publishState: sta })
      this.getJxList()
    },
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getList()
    },
    handleCurrentChange (newPage) {
      this.queryInfo.page = newPage
      this.getList()
    },
    // 获取简单的学科列表
    async getSleepList () {
      try {
        const { data: res } = await simple()
        this.simpleList = res
      } catch (err) {}
    },
    async secondaryDirectory (id) {
      // 获取二级目录
      const { data: res } = await direct({ subjectID: id })
      this.second = res
      this.queryInfo.catalogID = ''
      this.queryInfo.tags = ''
    },
    // 获取标签列表
    async getAble (id) {
      const { data: res } = await lable({ subjectID: id })
      this.lableList = res
    },
    // 获取录入人信息
    async getCreatorID () {
      try {
        const { data: res } = await people()
        this.CreatorIdList = res
      } catch (err) {}
    },
    City (id) {
      console.log(id)
      const cityList = citys(id)
      this.citys = cityList
    },
    // 点击取消让表单内容清空
    del () {
      this.queryInfo = {}
    },
    // 点击搜索
    async search () {
      this.getJxList()
    },
    // 点击删除
    delList (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove({ id: id })
        this.getContsiner()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch((err) => {
        console.dir(err)
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style scoped lang='scss'>
.container {
  .cont-fenju {
    margin-bottom: 15px;
    display: flex;
    justify-content: space-between;
    .car-span {
      font-size: 12px;
      color: pink;
    }
  }
  .cont-form {
    font-size: 12px;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
    .el-form-item {
      width: 25%;
      .el-select {
        width: 100%;
      }
      .select-inp {
        width: 50%;
        padding: 0 2px;
      }
    }
    .car-btn {
      display: flex;
      flex-direction: row-reverse;
    }
  }
  .slot-icon {
    background-color: #f4f4f5;
    color: #909399;
    height: 36px;
    font-size: 13px;
    line-height: 36px;
    padding-left: 25px;
    margin-bottom: 15px;
    .tab-span {
      margin-left: 10px;
    }
  }
  .block {
    display: flex;
    flex-direction: row-reverse;
    margin-right: 20px;
    margin-top: 20px;

  }
}
</style>
